﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace imarks.Utility
{
    public class EMailHelper
    {
        /// <summary>
        /// 邮件发送
        /// </summary>
        /// <param name="server">邮件服务器</param>
        /// <param name="emailTo">接收方邮箱地址</param>
        /// <param name="emailFrom">发送方邮箱地址</param>
        /// <param name="emailFromName">发送方显示名称</param>
        /// <param name="emailFromPwd">发送方邮箱密码</param>
        /// <param name="emailTitle">邮件标题</param>
        /// <param name="emailContent">邮件内容</param>
        /// <returns></returns>
        public static void SendEMail(string server, string emailTo, string emailFrom, string emailFromName, string emailFromPwd, string emailTitle, string emailContent)
        {
            MailMessage mail = new MailMessage();
            mail.BodyEncoding = System.Text.Encoding.Default;
            //设置邮件的发送级别
            mail.Priority = MailPriority.Normal;
            //设置邮件的格式
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;
            //发件人
            mail.From = new MailAddress(emailFrom, emailFromName);
            //收件人
            mail.To.Add(emailTo);
            mail.Subject = emailTitle;
            mail.Body = emailContent;

            SmtpClient client = new SmtpClient(server);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = new System.Net.NetworkCredential(mail.From.Address, emailFromPwd);//发件人

            client.Send(mail);
        }



        /// <summary>
        /// 邮件发送
        /// </summary>
        /// <param name="server">邮件服务器</param>
        /// <param name="port">邮件服务器端口</param>
        /// <param name="emailTo">接收方邮箱地址</param>
        /// <param name="emailFrom">发送方邮箱地址</param>
        /// <param name="emailFromName">发送方显示名称</param>
        /// <param name="emailFromPwd">发送方邮箱密码</param>
        /// <param name="emailTitle">邮件标题</param>
        /// <param name="emailContent">邮件内容</param>
        /// <returns></returns>
        public static void SendEMail(string server, int port, string emailTo, string emailFrom, string emailFromName, string emailFromPwd, string emailTitle, string emailContent)
        {
            MailMessage mail = new MailMessage();
            mail.BodyEncoding = System.Text.Encoding.Default;
            //设置邮件的发送级别
            mail.Priority = MailPriority.Normal;
            //设置邮件的格式
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;
            //发件人
            mail.From = new MailAddress(emailFrom, emailFromName);
            //收件人
            mail.To.Add(emailTo);
            mail.Subject = emailTitle;
            mail.Body = emailContent;

            SmtpClient client = new SmtpClient(server);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = new System.Net.NetworkCredential(mail.From.Address, emailFromPwd);//发件人
            client.Port = port;

            client.Send(mail);
        }

    }
}
